import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';


declare var $: any;
@Component({
	selector: 'sa-device-store',
	templateUrl: './device-store.component.html',
	styleUrls: ['./device-store.component.css']
})
export class DeviceStoreComponent implements OnInit {
	tableData = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	table: any = {
		status: '',
		searchKey: '',
		dataType: '1',
		productNo: '',
		iccid: '',
		startTime: '',
		endTime: '',
		deviceId: '',
		caseNo: ''
	}
	deviceStatusList: any = [{
		value: '',
		label: '全部'
	}, {
		value: 0,
		label: '库存'
	}, {
		value: 1,
		label: '已发货'
	}];
	limitsEdit = false;	//编辑权限按钮
	limitsDelete = false;	//删除权限按钮


	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;

			// 从信息汇总跳转过来的数据
			const summaryInfoParams = JSON.parse(common.getCookie('jumpFromSummaryInfo'))
			if (summaryInfoParams) {
			  this.table.deviceId = summaryInfoParams.deviceId
			  common.clearCookie('jumpFromSummaryInfo')
			}
		});
	}
	//初始化权限按钮
	initlimitsBtn() {
		let param = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_deviceStore:list')));
		if (param) {
			for (let item of param) {
				if (item.sourceKey === "deviceStore:edit" && item.parentId) {
					this.limitsEdit = true;
				};
				if (item.sourceKey === "deviceStore:delete" && item.parentId) {
					this.limitsDelete = true;
				};
			}
		}
	}
	ngOnInit() {
		this.initlimitsBtn();
		this.initDate();
		this.load();
	}
	//table刷新
	refresh() {
		this.load();
	}
	initDate() {
		var that = this;
		System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
			System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
				$("#datetimepicker1").datetimepicker({//时间插件
					format: "yyyy-mm-dd hh:ii:ss",
					language: "zh-CN",
					autoclose: true,
				}).on("changeDate", function (e) {
					var date = e.date;
					var time = common.getTimeMmss(date);
					$("#datetimepicker2").datetimepicker("setStartDate", time);
					that.table.startTime = time;
				});
				$("#datetimepicker2").datetimepicker({//时间插件
					format: "yyyy-mm-dd hh:ii:ss",
					language: "zh-CN",
					autoclose: true,
				}).on("changeDate", function (e) {
					var date = e.date;
					var time = common.getTimeMmss(date);
					$("#datetimepicker1").datetimepicker("setEndDate", time);
					that.table.endTime = time;
				});
				//					$("#datetimepicker3").datetimepicker({//时间插件
				//						format: "yyyy-mm-dd hh:ii:ss",
				//					    language: "zh-CN",
				//					    autoclose: true,
				//					}).on("changeDate", function(e){
				//						var date = e.date;
				//						var time = common.getTimeMmss(date);
				//						$("#datetimepicker4").datetimepicker("setStartDate", time);
				//						that.table.startTimeForSend = time;
				//					});
				//					$("#datetimepicker4").datetimepicker({//时间插件
				//						format: "yyyy-mm-dd hh:ii:ss",
				//					    language: "zh-CN",
				//					    autoclose: true,
				//					}).on("changeDate", function(e){
				//						var date = e.date;
				//						var time = common.getTimeMmss(date);
				//						$("#datetimepicker3").datetimepicker("setEndDate", time);
				//						that.table.endTimeForSend = time;
				//					});
			});
		}
		);
	}
	addRowDatas() {
		this.router.navigate(['/system/device-store/device-page'], { queryParams: { type: 'new' } });
	}

	editRowEvent(row) {
		this.router.navigate(['/system/device-store/device-page'], { queryParams: { id: row.id, type: 'updata', nowPage: this.curPage } });
	}
	//导出
	exportData() {
		this.table.searchKey = common.stringTrim(this.table.searchKey);
		var parameters = this.table;
		let timeCheck = common.correctTime(parameters, ['dataType'])
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'device/exportDeviceListFile.html')
	}
	//清除所选时间
	//	del_date_s(){
	//		this.table.startTimeForSend = '';
	//		this.table.endTimeForSend = '';
	//		$("#datetimepicker3").val('').change();
	//		$("#datetimepicker4").val('').change();
	//		$("#datetimepicker4").datetimepicker("setStartDate", null);
	//		$("#datetimepicker3").datetimepicker("setEndDate", null);
	//	}
	del_date_p() {
		this.table.startTime = '';
		this.table.endTime = '';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.load();
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				status: this.table.status,
				searchKey: common.stringTrim(this.table.searchKey),
				dataType: this.table.dataType,
				productNo: this.table.productNo,
				iccId: this.table.iccid,
				startTime: this.table.startTime,
				endTime: this.table.endTime,
				deviceId: this.table.deviceId,
				caseNo: this.table.caseNo
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("device/getDeviceInfoForPage.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	//删除
	delDeviceStore(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("device/deleteById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}


	fileUp: string = '';
	fileToUpload: File = null; //上传文件回调参数
	// 显示导入弹框
	showImportModal(modal) {
		// 清空选择的文件
		modal.show();
		this.fileToUpload = null;
	}
	// 关闭导入弹窗
	cancelUp(modal) {
		modal.hide();
		this.fileToUpload = null;
	}
	// 下载模板
	load_module() {
		var elemIF = document.createElement("iframe");
		elemIF.src = config.BaseURL + 'resources/template/库存管理模版.xlsx';
		elemIF.style.display = "none";
		document.body.appendChild(elemIF);
	}
	// 清空input type=file输入框的值
	clearFile() {
		this.fileUp = '';
	}
	//文件上传change回调
	fileChange(files: FileList) {
		this.fileToUpload = files.item(0);
		const isEXCEL = this.fileToUpload.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || this.fileToUpload.type === "application/vnd.ms-excel";
		if (!isEXCEL) {
			this.jsonApiService.message('上传文件只能是 excel格式!');
			this.fileToUpload = null;
			return;
		}
	}
	//导入submit
	submitData(modal) {
		if (!this.fileToUpload) {
			this.jsonApiService.message('请上传文件')
			return
		}
		const postParam = new FormData();
		postParam.append('file', this.fileToUpload);
		this.jsonApiService.form_http("device/importDeviceInfo.html", postParam).subscribe(data => {
			if (data.success) {
				modal.hide();
				this.fileToUpload = null;
				this.load()
			} else {
				this.jsonApiService.message(data.message)
			}
		})
	}

}